package trb.complexshield.bool.model;

import no.hrp.hvrc.utils.j3d.BBox;
import trb.complexshield.bool.IntersectableBox;
import trb.complexshield.bool.IntersectableCylinder;
import trb.complexshield.bool.IntersectableSphere;

/**
 * Utility that creates a bounding box from different bool primitives.
 */
public class AABBCreator {
    public static BBox createAABB(Object object) {
        if (object instanceof BoolBox) {
            BoolBox boolBox = (BoolBox) object;
            return new IntersectableBox(boolBox.lower, boolBox.upper, boolBox.l2w).getAABB();
        } else if (object instanceof BoolCylinder) {
            BoolCylinder boolCylinder = (BoolCylinder) object;
            return new IntersectableCylinder(boolCylinder.radius, boolCylinder.height, boolCylinder.l2w).getAABB();
        } else if (object instanceof BoolSphere) {
            BoolSphere boolSphere = (BoolSphere) object;
            return new IntersectableSphere(boolSphere.radius, boolSphere.l2w).getAABB();
        } else if (object instanceof BoolOr) {
            BBox aabb = null;
            BoolOr boolOr = (BoolOr) object;
            for (Object child : boolOr.children) {
                BBox childAabb = createAABB(child);
                if (childAabb != null) {
                    if (aabb == null) {
                        aabb = childAabb;
                    } else {
                        aabb.combine(childAabb);
                    }
                }
            }
            return aabb;
        } else if (object instanceof BoolSubtract) {
            BoolSubtract boolSubtract = (BoolSubtract) object;
            return createAABB(boolSubtract.a);
        }

        return null;
    }
}
